package pl.register.android.database.crud;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import pl.register.android.classes.Classes;
import pl.register.android.classes.ClassesTopic;
import pl.register.android.database.tables.ClassesDBTable;
import pl.register.android.lesson.Lesson;
import pl.register.android.rating.Rating;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-26
 */
public class ClassesCRUD {

    private SQLiteDatabase database;

    public ClassesCRUD(SQLiteDatabase database) {
        this.database = database;
    }

    public List<Classes> findClassesBy(Long studentLessonId, List<ClassesTopic> classesTopics) {
        List<Classes> classes = new ArrayList<Classes>();
        for (ClassesTopic classesTopic : classesTopics) {
            Classes classesByTopic = findClassesBy(studentLessonId, classesTopic);
            classes.add(classesByTopic);
        }
        return classes;
    }

    private Classes findClassesBy(Long lessonId, ClassesTopic classesTopic) {
        Classes classes = null;
        String where = ClassesDBTable.FK_LESSON_ID + " = " + lessonId +
                " AND " + ClassesDBTable.FK_CLASSES_TOPIC_ID + " = " + classesTopic.getId();
        Cursor cursor = database.query(ClassesDBTable.TABLE_NAME, null, where, null, null, null, null);
        if (cursor.moveToFirst()) {
            Long classesId = cursor.getLong(cursor.getColumnIndex(ClassesDBTable.ID));
            int classesPresenceInt = cursor.getInt(cursor.getColumnIndex(ClassesDBTable.PRESENCE));
            String classesRatingString = cursor.getString(cursor.getColumnIndex(ClassesDBTable.RATING));

            boolean presence = classesPresenceInt == 1;
            Rating rating = Rating.valueOf(classesRatingString);
            classes = new Classes(classesId, classesTopic, presence, rating);
        }
        cursor.close();
        return classes;
    }

    public Classes insertStudentClasses(Classes classes, ClassesTopic classesTopic, Lesson lesson) {
        ContentValues insertValues = new ContentValues();
        insertValues.put(ClassesDBTable.PRESENCE, classes.isPresence());
        insertValues.put(ClassesDBTable.RATING, classes.getRating().toString());
        insertValues.put(ClassesDBTable.FK_LESSON_ID, lesson.getId());
        insertValues.put(ClassesDBTable.FK_CLASSES_TOPIC_ID, classesTopic.getId());
        long rowId = database.insert(ClassesDBTable.TABLE_NAME, null, insertValues);
        classes.setId(rowId);
        return classes;
    }

    public Classes updateClasses(Classes classes) {
        String where = ClassesDBTable.ID + " = " + classes.getId();
        ContentValues updateValues = new ContentValues();
        updateValues.put(ClassesDBTable.PRESENCE, classes.isPresence());
        updateValues.put(ClassesDBTable.RATING, classes.getRating().toString());
        long rowId = database.update(ClassesDBTable.TABLE_NAME, updateValues, where, null);
        return classes;
    }

    public void delete(Lesson lesson) {
        String where = ClassesDBTable.FK_LESSON_ID + " = " + lesson.getId();
        int res = database.delete(ClassesDBTable.TABLE_NAME, where, null);
    }

}